package com.bz.common;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSON;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class HttpUtil {

    // 使用HttpClient 进行doGET请求
    public static String doGet(String url) {
        String result = null;
        // DefaultHttpClient httpclient =new DefaultHttpClient();//老版本的方法
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpGet httpget = new HttpGet(url);
        try {
            HttpResponse response = httpclient.execute(httpget);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                result = EntityUtils.toString(entity, "UTF-8");
            }
        } catch (Exception e) {
            LogUtils.error(log, e, "远程调用异常url:" + url);
        } finally {
            try {
                if (httpclient != null) {
                    httpclient.close();
                }
            } catch (IOException e) {
                LogUtils.error(log, e, "远程调用异常url:" + url);
            }
        }
        LogUtils.info(log, "远程调用url:" + url + ",res:" + result);
        return result;
    }

    // 使用HttpClient 进行dopOSTT请求,适合发送的数据为json数据格式
    public static String doPOST(String url, String outstr, Map<String, String> headerMap) {

        // return
        // DefaultHttpClient httpclient =new DefaultHttpClient();//老版本的方法
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httppost = new HttpPost(url);
        String result = null;
        try {
            httppost.setHeader("Content-Type", "application/json");
            if (MapUtils.isNotEmpty(headerMap)) {
                for (Entry<String, String> hm : headerMap.entrySet()) {
                    httppost.setHeader(hm.getKey(), hm.getValue());
                }
            }

            if (!StringUtils.isEmpty(outstr)) {
                httppost.setEntity(new StringEntity(outstr, "UTF-8"));
            }

            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                result = EntityUtils.toString(entity, "UTF-8");
            }

        } catch (Exception e) {
            LogUtils.error(log, e, "远程调用异常url:" + url);
        } finally {
            try {
                if (httpclient != null) {
                    httpclient.close();
                }
            } catch (IOException e) {
                LogUtils.error(log, e, "远程调用异常url:" + url);
            }
        }
        LogUtils.info(log, "远程调用url:" + url + ",param:,res:" + result);
        return result;
    }

    public static Map<String, Double> remoteAi(String url, String postJson) {
        try {
            String res = HttpUtil.doPOST(url, postJson, null);
            if (StringUtils.isEmpty(res)) {
                return null;
            }
            ResponseDto<Map<String, Object>> rs = JSON.parseObject(res, ResponseDto.class);
            // 200不属于异常，500属于异常
            if (200 == rs.getCode()) {
                // return ToolsUtils.getDouble(rs.getData());
                Map<String, Object> map = rs.getData();
                Map<String, Double> rm = new HashMap<>();
                rm.put(String.valueOf(map.get("classify")), ToolsUtils.getDoubleObject(map.get("x")));
                return rm;
            }
        } catch (Exception e) {
            LogUtils.error(log, e, "调用AI失败,url:" + url);
        }

        return null;
    }

    public static Double remoteHelth(String url, String postJson) {
        try {
            String res = HttpUtil.doPOST(url, postJson, null);
            if (StringUtils.isEmpty(res)) {
                return null;
            }
            ResponseDto<Double> rs = JSON.parseObject(res, ResponseDto.class);
            // 200不属于异常，500属于异常
            if (200 == rs.getCode()) {
                return ToolsUtils.getDoubleObject(rs.getData());
            } else {
                throw new BusinessException(rs.getMsg());
            }
        } catch (Exception e) {
            LogUtils.error(log, e, "调用设备健康度失败,url:" + url);
            throw new BusinessException("调用设备健康度失败!" + e.getMessage());
        }
    }
}
